package eerr.llrr.oorr.pte9983.uggtip9324;

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import eerr.llrr.oorr.pte9983.wifi4392.jump98001.RouteJump
import com.tencent.mmkv.MMKV

class TimeReceiver : BroadcastReceiver() {
    private val TAG = eerr.llrr.oorr.activitys.Constant3289.TAG
    val START_TIME_TURN = "start_time_turn"
    val TURN_TIME_GAP = 1 * 60 * 1000
    val TURN_DELAY_FOR_UNLCOK = 2 * 60 * 1000
    val MMKV_TIME_TURN_KEY = "mmkv_time_turn_key"

    var INTENAL_TIME = 30 * 60 * 1000

    fun postUpdateTime(turnTime: Int) {
        if (turnTime > 0) {
            INTENAL_TIME = turnTime * 60 * 1000
        }
    }

    companion object {
        var timeReceiver: TimeReceiver? = null
        fun getInstance(): TimeReceiver? {
            synchronized(this) {
                if (timeReceiver == null) {
                    timeReceiver = TimeReceiver()
                }
                return timeReceiver
            }
        }
    }

    fun register(context: Context) {
        // 暂时注释掉 用alerm 实现
        try {
            val intentFilterTimer = IntentFilter()
            intentFilterTimer.addAction(Intent.ACTION_SCREEN_OFF)
            intentFilterTimer.addAction(Intent.ACTION_USER_PRESENT)
            intentFilterTimer.addAction(Intent.ACTION_SCREEN_OFF)
            intentFilterTimer.addAction(START_TIME_TURN)
            context.registerReceiver(this, intentFilterTimer)

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun unregister(context: Context) {
        try {
            context.unregisterReceiver(this)
        } catch (e2: Exception) {
            e2.printStackTrace()
        }
    }

    override fun onReceive(context: Context, intent: Intent) {
        eerr.llrr.oorr.activitys.utim986.BLog.d(
            TAG,
            "onReceive() called with: context = $context, intent = $intent"
        )
        if (context == null) {
            eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "context is null")
            return
        }

        val action = intent?.action

        when (action) {
            Intent.ACTION_USER_PRESENT -> {
                delayForUnlock(context)
            }

            START_TIME_TURN -> {

            }
        }
    }

    private fun delayForUnlock(context: Context) {
        yyee.llee.mmee.pl190.host668.HandleUtils.remove(delayRunable)
        startTimerTurn(delay = TURN_DELAY_FOR_UNLCOK)
    }

    fun startTask(context: Context) {
        eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "start timer task")
        startTimerTurn(10 * 1000)
    }


    fun startTimerTurn(delay: Int) {
        eerr.llrr.oorr.activitys.utim986.BLog.d(
            TAG,
            " startTimerTurn() called with: context = , delay = $delay"
        )
        yyee.llee.mmee.pl190.host668.HandleUtils.postDelay(delayRunable, delay.toLong())
    }

    private val delayRunable = object : Runnable {
        override fun run() {
            if (!yyee.llee.mmee.pl190.host668.ScreenUtils.isScreenUnLock()) {
                eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "screen is lock")
                startTimerTurn(TURN_TIME_GAP)
                return
            }
            if (isTimerCanshow()) {
                eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "tu jump")
                RouteJump.safeJump(yyee.llee.mmee.pl190.host668.RouteKey.INTERVAL_AD)
            } else {
                eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "time not allow jump")
            }
            startTimerTurn(TURN_TIME_GAP)
        }

    }

    fun isTimerCanshow(): Boolean {
        val lastTime = MMKV.defaultMMKV().decodeLong(MMKV_TIME_TURN_KEY, 0L)
        val currentTimeMillis = System.currentTimeMillis()
        val timeGpa = currentTimeMillis - lastTime

        if (lastTime == 0L) {
            eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "first enter")
            MMKV.defaultMMKV().encode(MMKV_TIME_TURN_KEY, currentTimeMillis)
            return false
        }

        if (timeGpa >= INTENAL_TIME) {
            MMKV.defaultMMKV().encode(MMKV_TIME_TURN_KEY, currentTimeMillis)
            return true
        }
        return false
    }
}